<?php
/*
 * 帖子列表页
 */
session_start();
include_once dirname(__FILE__)."/src/daos/membership.php";
include_once dirname(__FILE__)."/src/daos/topicsdao.php";
include_once dirname(__FILE__)."/src/daos/topictypesdao.php";
include_once dirname(__FILE__)."/conf/smarty.conf.php";
include_once dirname(__FILE__)."/fckeditor/fckeditor.php";
if(isset($_POST["mobile"])){  // 手机模式
	exit();
}
elseif (isset($_POST['ajax'])) {	// ajax请求
	exit();
}
else{	// 正常显示页面
	$membership = new Membership();
	$topicsdao = new TopicsDAO();
	$user_id = 0;
	if(isset($_SESSION["user_id"])){		
		$user_id = intval($_SESSION["user_id"]);
	}
	$mng_role_id = 1;	// 管理组编号
	if(isset($_REQUEST["action"])&&isset($_REQUEST["topic_id"])){
		// 删除文章
		if($membership->isUserInRole($user_id, $mng_role_id)){// 验证权限
			$topic_id = intval($_REQUEST["topic_id"]);
			$topicsdao->deleteTopic($topic_id);
			$title = "删除文章";
			$info = "文章已删除";
			$url = "topiclist.php";			
			$smarty->assign("title",$title);
			$smarty->assign("info",$info);
			$smarty->assign("url",$url);
			$smarty->display("info.html");
		}
	}
	elseif (isset ($_POST["topic_title"])&&isset($_POST["topic_topictype_id"])&&isset($_POST["topic_content"])) {
		$topic = new Topic();
		$topic_title = strval($_POST["topic_title"]);
		$topic_topictype_id = intval($_POST["topic_topictype_id"]);
		$topic_content = strval($_POST["topic_content"]);
		date_default_timezone_set("Asia/Shanghai");
		$topic_datetime =  date("Y-m-d H:i:s");
		$topic->setTopicUserId($user_id);
		$topic->setTopicTitle($topic_title);
                $topic->setTopicContent($topic_content);
		$topic->setTopicDatetime($topic_datetime);
		$topic->setTopicTopicTypeId($topic_topictype_id);
		$topicsdao->insertTopic($topic);
		$title="发表帖子";
		$info="发表成功";
		$url="topiclist.php";
		$smarty->assign("title",$title);
		$smarty->assign("info",$info);
		$smarty->assign("url",$url);
		$smarty->display("info.html");		
	}
	else {
		$topics = $topicsdao->getAllTopics();
		$title = "帖子列表";
		$topictypesdao = new TopicTypesDAO();
		$topictypes = $topictypesdao->getAllTopicTypes();
		$editor = new FCKeditor('topic_content');
		$editor->BasePath = "fckeditor/";
		$editor->Height = "400px";
		$editor->Width = "100%";
		$editor->Value = null;
		$editor->ToolbarSet = "Basic";
		$smarty->assign("title",$title);
		$smarty->assign("topiclist",$topics);
		$smarty->assign("topictypelist",$topictypes);
		$smarty->assign("editor",$editor);
		$smarty->display("topiclist.html");	
	}
}
?>
